function dealkeyevent(keyCode, component) {
    components = component;
    switch (keyCode) {
        case 37:
            left_move();
            change_component(index_x, index_y);
            return false;
            break;
        case 39:
            right_move();
            change_component(index_x, index_y);
            return false;
            break;
        case 38:
            up_move();
            change_component(index_x, index_y);
            return false;
            break;
        case 40:
            down_move();
            change_component(index_x, index_y);
            return false;
            break;
    }
}
function left_move() {
    index_y--;
    if (index_y < 0) {
        index_y = components[index_x].length - 1;

    }
}
function right_move() {
    index_y++;
    if (index_y > components[index_x].length - 1) {
        index_y = 0;
    }
}
function up_move() {
    index_x--;
    if (index_x < 0) {
        index_x = components.length - 1;
    }
    if (index_y > components[index_x].length - 1) {
        index_y = components[index_x].length - 1;
    }
}
function down_move() {
    index_x++;
    if (index_x > components.length - 1) {
        index_x = 0;
    }
    if (index_y > components[index_x].length - 1) {
        index_y = components[index_x].length - 1;
    }
}
function deal_key_event(event) {
    if (event.keyCode == 40 && components == keyboardArray && index_x == keyboardArray.length - 1) {
        components = operateArray;
    }
    if (event.keyCode == 38 && components == operateArray) {
        components = keyboardArray;
        index_x = 3;
    }
    if (event.keyCode == 38 && components == keyboardArray && index_x == 0 && index_y <= 10 && selectWordArray[0].length > 1) {
        components = selectWordArray;
    }
    if (event.keyCode == 40 && components == selectWordArray) {
        components = keyboardArray;
        index_x = -1;
    }
    if (event.keyCode == 40 && components == pageArray) {
        components = keyboardArray;
        index_x = -1;
        index_y = 13;
    }
    if (event.keyCode == 38 && components == keyboardArray && index_x == 0 && index_y >= 10) {
        components = pageArray;
        index_x = 0;
        index_y = 0;
    }
    if (event.keyCode == 39 && components == selectWordArray && index_y == selectWordArray[0].length - 1) {
        components = pageArray;
        index_x = 0;
        index_y = 0;
    }
    if (event.keyCode == 37 && components == pageArray && index_y == 1 && selectWordArray[0].length > 1) {
        components = selectWordArray;
        index_y = selectWordArray[0].length;
    }
}

document.onkeydown = function (event) {
    if (inputFlag) {
        deal_key_event(event);
        dealkeyevent(event.keyCode, components);
    }
};

var components_positions = [];
function init_x_y(x, y) {
    index_x = x;
    index_y = y;

}
function save_old_index() {
    components_positions[components_positions.length] = [index_x, index_y, components];
}
function recover_old_index() {
    var components_position = components_positions.pop();
    index_x = components_position[0];
    index_y = components_position[1];
    components = components_position[2];
    $("#" + components[index_x][index_y]).focus();
}
function remove_old_index() {
    components_positions.pop();
}
function change_component(i, j) {
    document.getElementById(components[i][j]).focus();
}
function change_components(new_index_x, new_index_y, new_components) {
    components = new_components;
    index_x = new_index_x;
    index_y = new_index_y;
    $("#" + components[index_x][index_y]).focus();
}

var index_x = 0;
var index_y = 0;
init_x_y(index_x, index_y);
var inputLetter = false;

function select_word(e, element, action) {
    if (isChinese == 1) {
        if (action == "leave") {
            $("#" + element).attr("class", "select_words");
        } else if (action == "focus") {
            $("#" + element).attr("class", element.substring(0, 12) + "_over");
        }
        else if (action == "click" && (e == '' || e && e.keyCode == 13)) {
            inputLetter = true;
            letters = "";
            $("#input_letters").empty();
            charContainer = $("#" + element).text().replace(/(^\s*)|(\s*$)/g, "").split("\.")[1];
        }
    }
}

function select_EN_word(value) {
    if (document.activeElement.id != 'keyboard_dele') {
        inputLetter = true;
    }
    if (isChinese == 0) {
        charContainer = value.substr(value.length - 1);
    }
}